package com.task.control.slice;

import com.task.control.ResourceTable;
import com.task.control.service.ServiceAbility;
import com.task.control.task.TaskDispatch;
import com.task.controller.annotation.AsyncTask;
import com.task.controller.trusteeship.TaskRegister;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Text;

public class MainAbilitySlice extends AbilitySlice implements TaskRegister {

    private Text text;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        text = findComponentById(ResourceTable.Id_text_helloworld);
        text.setClickedListener(component -> TaskDispatch.getInstance().execute("notify/task"));
    }

    @Override
    public void onActive() {
        super.onActive();
        registerTask(this);
        Intent.OperationBuilder operationBuilder = new Intent.OperationBuilder();
        Operation operation = operationBuilder.withDeviceId("")
                .withAbilityName(ServiceAbility.class)
                .withBundleName(getBundleName()).build();
        Intent i = new Intent();
        i.setOperation(operation);
        startAbility(i);
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    protected void onStop() {
        super.onStop();
        unregister(this);
    }

    @AsyncTask(value = "notify/task", isAsync = true)
    private void notifyTask() {
        TaskDispatch taskDispatch = TaskDispatch.getInstance();
        taskDispatch.init(this);
        taskDispatch.execute("async/param", this, "有参异步调用");
        taskDispatch.execute("current/param", this, 3637);
        taskDispatch.execute("ui/param", this, 189.67678);

        taskDispatch.execute("async/test");
        taskDispatch.execute("current/test");
        taskDispatch.execute("ui/test");
    }
}
